import request from '@/utils/request'
import { Message } from 'element-ui'

function getDict() {
  return request({
    url: '/dictInfo/all',
    method: 'get'
  })
}


export  function setDict() {
    getDict().then(response =>{
        sessionStorage.setItem("dictList",JSON.stringify(response.data)); // 存入sessionStorage中
    }).catch(error =>{
        Message.error(error.message || '获取字典失败')
    })
    
}

export function removeDict() {
    sessionStorage.removeItem("dictList"); // 删除字典
}

export function dictValueFilter(dictCode,dictType){
    let result
    const array = JSON.parse(sessionStorage.getItem("dictList"));
    array.forEach(element => {
        if(element.dictCode == dictType){
            const dict = element.children;
            dict.forEach(element => {
                if(element.dictCode == dictCode){
                    result = element.dictValue;
                }
            });
        }
    });
    return result;
}

export function dictArray(dictType){
    let result
    const array = JSON.parse(sessionStorage.getItem("dictList"));
    array.forEach(element => {
        if(element.dictCode == dictType){
            result = element.children;
        }
    })
    return result
}